import { requestClient } from '#/api/request';

export interface GirdDTO {
  id?: number;
  userId?: number;
  title: string;
  coverImage?: string;
  description?: string;
  status?: number;
  position?: number;
  userMembershipId: number;
  reminderSent?: boolean;
  viewCount?: number;
  pageNum?: number;
  pageSize?: number;
}

export interface GirdVO {
  id: number;
  userId: number;
  title: string;
  coverImage?: string;
  coverImageUrl?: string;
  description?: string;
  status: number;
  position?: number;
  userMembershipId: number;
  reminderSent: boolean;
  viewCount: number;
  levelId?: number;
  levelName?: string;
  expireTime?: string;
  gridWidth?: number;
  gridHeight?: number;
  createdTime: string;
  updateTime: string;
}

export interface GirdPageResult {
  records?: GirdVO[];
  result?: GirdVO[];
  total: number;
  size?: number;
  pageSize?: number;
  current?: number;
  pageNo?: number;
  pages?: number;
  totalPages?: number;
  start?: number;
  end?: number;
}

export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
}

export namespace GirdApi {
  /**
   * 分页查询格子
   */
  export function getGirdPage(params?: Partial<GirdDTO>): Promise<GirdPageResult> {
    return requestClient.get('/gird/page', { params });
  }

  /**
   * 获取格子详情
   */
  export function getGirdById(id: number): Promise<GirdVO> {
    return requestClient.get(`/gird/${id}`);
  }

  /**
   * 新增格子
   */
  export function createGird(data: Omit<GirdDTO, 'id'>): Promise<boolean> {
    return requestClient.post('/gird', data);
  }

  /**
   * 修改格子
   */
  export function updateGird(data: GirdDTO): Promise<boolean> {
    return requestClient.put('/gird', data);
  }

  /**
   * 删除格子
   */
  export function deleteGird(id: number): Promise<boolean> {
    return requestClient.delete(`/gird/${id}`);
  }

  /**
   * 批量删除格子
   */
  export function batchDeleteGird(ids: number[]): Promise<boolean> {
    return requestClient.delete('/gird/batch', { data: ids });
  }

  /**
   * 根据用户ID获取格子列表
   */
  export function getGirdByUserId(userId: number): Promise<GirdVO[]> {
    return requestClient.get(`/gird/user/${userId}`);
  }

  /**
   * 上传格子封面图片
   */
  export function uploadCoverImage(file: File): Promise<string> {
    const formData = new FormData();
    formData.append('file', file);
    return requestClient.post('/gird/upload/cover', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
  }

  /**
   * 删除格子封面图片
   */
  export function deleteCoverImage(fileName: string): Promise<boolean> {
    return requestClient.delete(`/gird/delete/cover/${fileName}`);
  }
}

// 导出默认的 API 对象
export const girdApi = GirdApi;
